
package tableur;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

import structure.DataCenter;

/**
 * Gestionnaire de tables.
 * 
 * @author David Bernard
 * @since java 1.6
 */
public class TableManager
{
	private DataCenter dataCenter = null;
	private JTable mainTable = null;
	private JTable leftTable = null;
	private JScrollPane scrollPane = null;
	private ArrayList<TableListener> ecouteurs = null;

	/**
	 * Gère l'implentation graphique des tables.
	 * 
	 */
	public TableManager()
	{
		this.ecouteurs = new ArrayList<TableListener>();

		this.mainTable = new MainTable();
		this.leftTable = new LeftTable(this.mainTable);

		this.scrollPane = new JScrollPane(this.mainTable);
		this.scrollPane.setRowHeaderView(this.leftTable);
	}

	/**
	 * Retourne le composant graphique contenant la table.
	 * 
	 * @return Container, le composant graphique.
	 */
	public Container getPane()
	{
		return this.scrollPane;
	}

	/**
	 * Ajoute un ecouteur de table.
	 * 
	 * @param listener TableListener l'écouteur de table concerné.
	 */
	public void addTableListener(TableListener listener)
	{
		this.ecouteurs.add(listener);
	}

	/**
	 * Setteur de dataCenter
	 * 
	 * @param dataCenter DataCenter, le dataCenter désiré.
	 */
	public void setDataCenter(DataCenter dataCenter)
	{
		this.dataCenter = dataCenter;
	}

	private void fireUpdated()
	{
		ListIterator<TableListener> ite = this.ecouteurs.listIterator();
		while (ite.hasNext())
		{
			ite.next().dataModifie();
		}
	}

	private class MainTable extends JTable
	{
		private MainTable()
		{
			super(new MainTableModel());

			this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			this.setPreferredScrollableViewportSize(new Dimension(800, 500));
			this.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			this.setCellSelectionEnabled(true);
			this.tableHeader.setEnabled(false);
			this.setGridColor(Color.BLACK);
		}
	}

	private class LeftTable extends JTable
	{
		private LeftTable(JTable mainTable)
		{
			super(new LeftTableModel());
			Dimension dim = null;

			dim = this.getPreferredScrollableViewportSize();
			dim.width = 30;
			this.setPreferredScrollableViewportSize(dim);
			this.setRowHeight(mainTable.getRowHeight());
			this.setEnabled(false);
			this.setGridColor(Color.BLACK);
		}
	}

	// Classe interne propre au fonction du tableau
	private class MainTableModel extends AbstractTableModel
	{
		@Override
		public int getColumnCount()
		{
			return Tableur.MAX_COLONNE;
		}

		@Override
		public int getRowCount()
		{
			return Tableur.MAX_LIGNE;
		}

		@Override
		public String getColumnName(int col)
		{
			String s = "";

			while (col >= 0)
			{
				s = (char) (col % 26 + 'A') + s;
				col = col / 26 - 1;
			}

			return s;
		}

		@Override
		public Object getValueAt(int row, int col)
		{
			return TableManager.this.dataCenter.get(row, col);
		}

		@Override
		public boolean isCellEditable(int row, int col)
		{
			// Note that the data/cell address is constant,
			// no matter where the cell appears onscreen.
			return true;
		}

		@Override
		public void setValueAt(Object value, int row, int col)
		{
			TableManager.this.dataCenter.set(row, col, value);
			this.fireTableCellUpdated(row, col);
			TableManager.this.fireUpdated();
		}
	}

	/**
	 * Classe interne propre au fonction du tableau
	 * 
	 */
	private class LeftTableModel extends AbstractTableModel
	{
		@Override
		public int getColumnCount()
		{
			return 1;
		}

		@Override
		public int getRowCount()
		{
			return Tableur.MAX_LIGNE;
		}

		@Override
		public String getColumnName(int col)
		{
			return " ";
		}

		@Override
		public Object getValueAt(int row, int col)
		{
			return row + 1;
		}

		@Override
		public boolean isCellEditable(int row, int col)
		{
			return false;
		}
	}
}
